home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
TPUG - Toronto PET Users Group
/
TPUG Users Group CD
/
TPUG Users Group CD.iso
/
AMIGA
/
AMICUS
/
AMIBEST1.ADF
/
AmigaBasicStuff
/
FutureSound
/
Example.BAS
< prev
next >
Wrap
BASIC Source File
|
1987-07-22
|
6KB
|
225 lines
' Example of Loading and Playing Sounds with the FutureSound
' Copyright 1986 by Applied Visions
' Applied Visions
' 15 Oak Ridge Road
' Medford, Mass. 02155
' (617) 488-3602
' Program and text by John Foust, 22-Jun-86
' ATTENTION:
' Please note the files 'future.library', 'future.bmap' and 'exec.bmap'
' must be present for this program to work.
' We only want to declare these functions once.
IF FuncDecl = 0 THEN
DECLARE FUNCTION FSGetSize& LIBRARY
DECLARE FUNCTION FSLoadSound& LIBRARY
DECLARE FUNCTION FSPlaySound& LIBRARY
DECLARE FUNCTION FSStopSound& LIBRARY
DECLARE FUNCTION AllocMem& LIBRARY
FuncDecl = 1
END IF
' This tells Amiga Basic we want to use these libraries.
' It will look for them in this directory and the LIBS:
' directory, which is usually on your Workbench disk.
' The FutureSound library
LIBRARY "future.library"
' The 'exec' library for AmigaDOS functions
LIBRARY "exec.library"
' Get a sound file name from the user.
FileEntry:
PRINT
PRINT "Enter the full name of a sound file > ";
INPUT SoundName$
PRINT
PRINT "Looking for ";SoundName$
PRINT
' Add the character number ZERO to the end of the string,
' since all C strings are terminated with a zero character,
' and the library contains C functions.
SoundName$ = SoundName$+CHR$(0)
'Call the 'future.library' FSGetSize& function to get the size of
'a given sound file. The address of the zero-terminated string
'is sent to the function, using the Amiga Basic SADD() function.
'If the FSGetSize& function returns zero, then it could not open
'that file for some reason.
'Chances are, they typed the name incorrectly or that file is
'not on this disk.
SoundSize& = FSGetSize&(SADD(SoundName$))
IF SoundSize& = 0 THEN
PRINT "That file is not present, please try another."
GOTO FileEntry
END IF
PRINT "The size of that file is"; SoundSize&; "bytes."
'The sound file exists, so try to load it into memory.
'We must ask the operating system for memory that can be used
'by the Amiga's sound chips. We ask for this memory to be cleared
'to all zeroes, too.
'If there is not enough memory available in this computer
'to load this sound, this function returns zero, so warn the user.
'If we already have some memory, then free it before asking for more.
'The MemTry& would be non-zero if there was memory allocated.
IF MemTry& <> 0 THEN
GOSUB FreeUpMem
END IF
'This number, 65538, tells AmigaDOS what type of memory to allocate.
'This is MEMF_CHIP plus MEMF_CLEAR, if you know what those are.
MemType& = 65538&
MemTry& = AllocMem&(SoundSize&,MemType&)
'Reset the memory size indicator
MemSize& = 0
IF MemTry& = 0 THEN
PRINT "There is not enough memory to load that sound."
'Tell them how big the sound could be, using another
'exec.library' function.
MaxSize& = AvailMem&(MemType&)
PRINT "The largest sound you can load now is";
PRINT MaxSize&; "bytes in size."
PRINT "Please try again."
GOTO FileEntry
END IF
'Since the AllocMem() succeeeded, remember how much memory to free later.
MemSize& = SoundSize&
'We have allocated a large section of memory.
'Now ask the 'future.library' to load the sound.
'If this function returns zero, then something went wrong.
'If the sound loads correctly, it returns the recording rate,
'in hertz.
RecRate& = FSLoadSound&(SADD(SoundName$),MemTry&)
PRINT "The sound was recorded at"; RecRate&; "hertz."
IF RecRate& = 0 THEN
PRINT "Something went wrong in loading this sound."
PRINT "Please try again."
GOSUB FreeUpMem
GOTO FileEntry
END IF
'We have loaded the sound.
'Now, we can play it.
'This function takes several arguments.
'BufLen& represents how much of the sound to play.
'It represents the number of one-byte samples to scan per period.
' Set it to SoundSize& to play the whole sound.
' Set it to less than SoundSize& to hear only the first
' part of the sound.
BufLen& = SoundSize&
PRINT "The BufLen& is"; BufLen&
'Reps& is a counter, telling how many times to repeat the sound.
'Zero means forever, 1 means once.
Reps& = 1
PRINT "The sound will repeat";
IF Reps& = 0 THEN
PRINT "forever."
ELSE
PRINT Reps&; "times."
END IF
'Period& can be found by dividing the magic number
'3579545& by the recording rate. This should be a minimum of 124.
Period& = INT(3579545& / RecRate&)
PRINT "The Period is"; Period&
'The last argument is the volume, from 0 to 64
Vol& = 64
PRINT "The Volume is"; Vol&
PlayIt:
Result& = FSPlaySound&(MemTry&,BufLen&,Reps&,Period&,Vol&)
PRINT "Play this sound again? Enter Y or N. > ";
INPUT Again$
'Only stop a sound once!
GOSUB StopIt
IF Again$ = "Y" OR Again$ = "y" THEN
GOTO PlayIt
END IF
PRINT "Play another sound? Enter Y or N. > ";
INPUT Again$
IF Again$ = "Y" OR Again$ = "y" THEN
GOTO FileEntry
END IF
'Otherwise free its memory, and stop the program.
GOSUB FreeUpMem
GOTO EndOfIt
FreeUpMem:
PRINT "Freeing the memory used by the sound."
'Since we do not need this memory any more, we should
'return it to the operating system with the FreeMem function.
'Be sure not to free memory that is free already.
IF MemTry& = 0 THEN GOTO ForgetIt
'FreeMem() takes two arguments, a pointer to the memory - MemTry&
'and the size of the memory - MemSize&
CALL FreeMem(MemTry&,MemSize&)
ForgetIt:
RETURN
'Another function can stop the sound before it finishes.
'Its only argument is the value returned from FSPlaySound&
'THIS ROUTINE SHOULD BE CALLED ONLY ONCE PER CALL OF FSPlaySound !!!
StopIt:
PRINT "Halting the sound."
HaltIt& = FSStopSound&(Result&)
RETURN
'The end of the program.
EndOfIt:
PRINT "End of program."
LIBRARY CLOSE
PRINT "Library closed."
END